package com.common.helper;


import com.common.annotation.Helper;
import com.common.endpoint.MetricsEndpoint;
import com.google.common.base.Predicates;
import com.system.vo.response.Server;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Helper
public class ActuatorHelper {

    private static final BigDecimal DECIMAL = new BigDecimal("1048576");

    @Autowired
    private MetricsEndpoint metricsEndpoint;

    public List<MetricsEndpoint.FebsMetricResponse> getMetricResponseByType(String type) {
        MetricsEndpoint.ListNamesResponse listNames = metricsEndpoint.listNames();
        Set<String> names = listNames.getNames();
        Iterable<String> jvm = names.stream()
                .filter(Predicates.containsPattern(type)::apply)
                .collect(Collectors.toList());
        List<MetricsEndpoint.FebsMetricResponse> metricResponseList = new ArrayList<>();
        jvm.forEach(s -> {
            MetricsEndpoint.FebsMetricResponse metric = metricsEndpoint.metric(s, null);
            metricResponseList.add(metric);
        });
        return metricResponseList;
    }

    /*
    获取tomcat的信息
     */
    public List<Server> getTomcatInfoFromMetricData(List<MetricsEndpoint.FebsMetricResponse> metrics) {
        List<Server> tomcats = new ArrayList<>();
        metrics.forEach(d -> {
            Server tomcatInfo = new Server();
            String name = d.getName();
            MetricsEndpoint.Sample sample = d.getMeasurements().get(0);
            Double value = sample.getValue();
            switch (name) {
                case "tomcat.sessions.created":
                    tomcatInfo.setParamName("tomcat.sessions.created");
                    tomcatInfo.setDescribe("tomcat 已创建 session 数");
                    tomcatInfo.setValue(value);
                    tomcats.add(tomcatInfo);
                    break;
                case "tomcat.sessions.expired":
                    tomcatInfo.setParamName("tomcat.sessions.expired");
                    tomcatInfo.setDescribe("tomcat 已过期 session 数");
                    tomcatInfo.setValue(value);
                    tomcats.add(tomcatInfo);
                    break;
                case "tomcat.sessions.active.current":
                    tomcatInfo.setParamName("tomcat.sessions.active.current");
                    tomcatInfo.setDescribe("tomcat 当前活跃 session 数");
                    tomcatInfo.setValue(value);
                    tomcats.add(tomcatInfo);
                    break;
                case "tomcat.sessions.active.max":
                    tomcatInfo.setParamName("tomcat.sessions.active.max");
                    tomcatInfo.setDescribe("tomcat 活跃 session 数峰值");
                    tomcatInfo.setValue(value);
                    tomcats.add(tomcatInfo);
                    break;
                case "tomcat.sessions.rejected":
                    tomcatInfo.setParamName("tomcat.sessions.rejected");
                    tomcatInfo.setDescribe("超过session 最大配置后，拒绝的 session 个数");
                    tomcatInfo.setValue(value);
                    tomcats.add(tomcatInfo);
                    break;
                default:
            }
        });
        return tomcats;
    }

    public List<Server> getJvmInfoFromMetricData(List<MetricsEndpoint.FebsMetricResponse> metrics) {
        List<Server> jvmInfos = new ArrayList<>();
        metrics.forEach(d -> {
            Server jvmInfo = new Server();
            String name = d.getName();
            MetricsEndpoint.Sample sample = d.getMeasurements().get(0);
            Double value = sample.getValue();
            switch (name) {
                case "jvm.memory.max":
                    jvmInfo.setParamName("jvm.memory.max");
                    jvmInfo.setDescribe("JVM 最大内存");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.memory.committed":
                    jvmInfo.setParamName("jvm.memory.committed");
                    jvmInfo.setDescribe("JVM 可用内存");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.memory.used":
                    jvmInfo.setParamName("jvm.memory.used");
                    jvmInfo.setDescribe("JVM 已用内存");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.buffer.memory.used":
                    jvmInfo.setParamName("jvm.buffer.memory.used");
                    jvmInfo.setDescribe("JVM 缓冲区已用内存");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.buffer.count":
                    jvmInfo.setParamName("jvm.buffer.count");
                    jvmInfo.setDescribe("当前缓冲区数量");
                    jvmInfo.setValue(value);
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.threads.daemon":
                    jvmInfo.setParamName("jvm.threads.daemon");
                    jvmInfo.setDescribe("JVM 守护线程数量");
                    jvmInfo.setValue(value);
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.threads.live":
                    jvmInfo.setParamName("jvm.threads.live");
                    jvmInfo.setDescribe("JVM 当前活跃线程数量");
                    jvmInfo.setValue(value);
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.threads.peak":
                    jvmInfo.setParamName("jvm.threads.peak");
                    jvmInfo.setDescribe("JVM 峰值线程数量");
                    jvmInfo.setValue(value);
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.classes.loaded":
                    jvmInfo.setParamName("jvm.classes.loaded");
                    jvmInfo.setDescribe("GC 时, 年轻代分配的内存空间");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.classes.unloaded":
                    jvmInfo.setParamName("jvm.classes.unloaded");
                    jvmInfo.setDescribe("GC 时, 老年代分配的内存空间");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.gc.memory.allocated":
                    jvmInfo.setParamName("jvm.gc.memory.allocated");
                    jvmInfo.setDescribe("GC 时, 老年代的最大内存空间");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.gc.memory.promoted":
                    jvmInfo.setParamName("jvm.gc.memory.promoted");
                    jvmInfo.setDescribe("FullGC 时, 老年代的内存空间");
                    jvmInfo.setValue((value/1000000));
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.gc.max.data.size":
                    jvmInfo.setParamName("jvm.gc.max.data.size");
                    jvmInfo.setDescribe("JVM 已加载 Class 数量");
                    jvmInfo.setValue(value);
                    jvmInfos.add(jvmInfo);
                    break;
                case "jvm.gc.live.data.size":
                    jvmInfo.setParamName("jvm.gc.live.data.size");
                    jvmInfo.setDescribe("JVM 未加载 Class 数量");
                    jvmInfo.setValue(value);
                    jvmInfos.add(jvmInfo);
                    break;
                default:
            }
        });
        return jvmInfos;
    }

}
